﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ApplicationLibrary;
using Plumber.Database;
using ApplicationLibrary.DBManager;
using Plumber.Masters;
using Plumber.Masters.Reports.DataSet;
using ApplicationLibrary.SqlConnector;
using Plumber.Sales;

namespace Plumber.Masters.Reports
{
    public partial class EnvelopePrint : ApplicationLibrary.DBManager.ReportForm
    {
        Sales.Forms.SalesEnumsManager TypeTypes = null;
        DRMasters drMasters = null;
        DataReaderUpdater drTitle = null;

        public EnvelopePrint()
            : base(Properties.Resources.FN_SalesSummary)
        {
            InitializeComponent();
        }

        protected override void OnBatchExecute(string reportName)
        {
            if (rdbFromInvoice.Checked)
                PrintEnvelopeFromInvoice();

            //qb.AddSelectAll(PL_SalesInvoices.Name);
            //qb.AddFrom(PL_SalesInvoices.Name);

            //if (rdbFromMaster.Checked)
            //{
            //    SqlABParameter sType = new SqlABParameter(base.providerType, "@p8", SqlDbType.Int);
            //    sType.Value = TypeTypes.GetIntValue();
            //    scc.Parameters.Add(sType);
            //    qb.AddCompare(PL_SalesInvoices.Type, sType);
            //}

            //if (rdbSelectAdmin.Checked)
            //{
            //    SqlABParameter sFrom = new SqlABParameter(base.providerType, "@p3", SqlDbType.NVarChar, PL_SalesInvoices.Administrator.Len);
            //    sFrom.Value = rdrAdminFrom.Text;
            //    scc.Parameters.Add(sFrom);
            //    SqlABParameter sTo = new SqlABParameter(base.providerType, "@p4", SqlDbType.NVarChar, PL_SalesInvoices.Administrator.Len);
            //    sTo.Value = rdrAdminTo.Text;
            //    scc.Parameters.Add(sTo);
            //    qb.AddBetween(PL_SalesInvoices.Administrator, sFrom, sTo);
            //    SqlABParameter sNo = new SqlABParameter(base.providerType, "@p5", SqlDbType.NVarChar, PL_SalesInvoices.Administrator.Len);
            //    sNo.Value = "";
            //    scc.Parameters.Add(sNo);
            //    qb.AddCompare(PL_SalesInvoices.Administrator, sNo, "<>");
            //}

            //if (rdbSelectMaster.Checked && rdbFromMaster.Checked)
            //{
            //    SqlABParameter sFrom = new SqlABParameter(base.providerType, "@p6", SqlDbType.NVarChar, PL_SalesInvoices.Master.Len);
            //    sFrom.Value = rdrMasterFrom.Text;
            //    scc.Parameters.Add(sFrom);
            //    SqlABParameter sTo = new SqlABParameter(base.providerType, "@p7", SqlDbType.NVarChar, PL_SalesInvoices.Master.Len);
            //    sTo.Value = rdrMasterTo.Text;
            //    scc.Parameters.Add(sTo);
            //    qb.AddBetween(PL_SalesInvoices.Master, sFrom, sTo);
            //}

            //qb.AddOrder(PL_SalesInvoices.Administrator);
            //qb.AddOrder(PL_SalesInvoices.ReleaseDate);
            //qb.AddOrder(PL_SalesInvoices.InvoiceNo);

            //scc.CommandText = qb.Query;

            //SqlABDataReader dr = scc.ExecuteReader();

            //List<DSInvoiceSummaryMaster> Master = new List<DSInvoiceSummaryMaster>();
            //List<DSInvoiceSummaryDetail> Detail = new List<DSInvoiceSummaryDetail>();

            //DSInvoiceSummary DSInvSum = new DSInvoiceSummary();

            //string oldcode = null;
            //while (dr.Read())
            //{
            //    string code = "";
            //    MastersType typeAnalized;
            //    switch (TypeTypes.GetValue())
            //    {
            //        case MastersType.T_Condominium:
            //            typeAnalized = MastersType.T_Administrator;
            //            code = dr.GetString(PL_SalesInvoices.Administrator);
            //            break;
            //        default:
            //            typeAnalized = TypeTypes.GetValue();
            //            code = dr.GetString(PL_SalesInvoices.Master);
            //            break;
            //    }
            //    string desc = "";

            //    if (code != oldcode)
            //    {
            //        oldcode = code;
            //        if (code != "")
            //        {
            //            if (drMasters.Find(typeAnalized, code))
            //            {
            //                desc = drMasters.GetStringColumn(PL_Masters.Description);
            //                string address = drMasters.GetStringColumn(PL_Masters.Address);
            //                string zip = drMasters.GetStringColumn(PL_Masters.ZipCode);
            //                string city = drMasters.GetStringColumn(PL_Masters.City);
            //                string county = drMasters.GetStringColumn(PL_Masters.County);
            //                if (drTitle.Find(drMasters.GetStringColumn(PL_Masters.Title)))
            //                    desc = drTitle.GetStringColumn(PL_Titles.Description) + "\n" + desc;

            //                desc = string.Concat(desc, "\n", address, "\n", zip, " ", city, " ", county);
            //            }
            //        }

            //        DSInvoiceSummary.HeaderRow dHeader = DSInvSum.Header.NewHeaderRow();
            //        dHeader["Code"] = code;
            //        dHeader["Description"] = desc;
            //        DSInvSum.Header.AddHeaderRow(dHeader);

            //        DSInvoiceSummaryMaster master = new DSInvoiceSummaryMaster();
            //        master.Code = code;
            //        master.Description = desc;
            //        Master.Add(master);
            //    }

            //    // Add Detail
            //    DSInvoiceSummaryDetail detail = new DSInvoiceSummaryDetail();
            //    detail.Code = code;

            //    detail.Description = dr.GetInt32(PL_SalesInvoices.Type) == (int)MastersType.T_Condominium
            //                    ? dr.GetString(PL_SalesInvoices.Address)
            //                    : dr.GetString(PL_SalesInvoices.Description);
            //    detail.SalesNo = dr.GetString(PL_SalesInvoices.InvoiceNo);
            //    detail.RestraintAmount = dr.GetFloat(PL_SalesInvoices.RestraintAmount);
            //    detail.Total = dr.GetFloat(PL_SalesInvoices.Total) - detail.RestraintAmount;
            //    detail.Amount = dr.GetFloat(PL_SalesInvoices.Amount);
            //    detail.Tax = dr.GetFloat(PL_SalesInvoices.Tax);
            //    detail.TaxCode = dr.GetInt32(PL_SalesInvoices.TaxCode) == 0
            //                    ? ""
            //                    : dr.GetInt32(PL_SalesInvoices.TaxCode).ToString() + " %";
            //    Detail.Add(detail);


            //    DSInvoiceSummary.RowsRow dRow = DSInvSum.Rows.NewRowsRow();
            //    dRow["Code"] = code;
            //    dRow["ReleaseDate"] = dr.GetDateTime(PL_SalesInvoices.ReleaseDate); ;
            //    dRow["Description"] = dr.GetInt32(PL_SalesInvoices.Type) == (int)MastersType.T_Condominium
            //                    ? dr.GetString(PL_SalesInvoices.Address)
            //                    : dr.GetString(PL_SalesInvoices.Description);
            //    dRow["SaleNo"] = dr.GetString(PL_SalesInvoices.InvoiceNo);
            //    dRow["RestraintAmount"] = dr.GetFloat(PL_SalesInvoices.RestraintAmount);
            //    dRow["Total"] = dr.GetFloat(PL_SalesInvoices.Total) - detail.RestraintAmount;
            //    dRow["Amount"] = dr.GetFloat(PL_SalesInvoices.Amount);
            //    dRow["Tax"] = dr.GetFloat(PL_SalesInvoices.Tax);
            //    dRow["TaxCode"] = dr.GetInt32(PL_SalesInvoices.TaxCode) == 0
            //                    ? ""
            //                    : dr.GetInt32(PL_SalesInvoices.TaxCode).ToString() + " %";

            //    DSInvSum.Rows.AddRowsRow(dRow);
            //}

            //if (rdbFromInvoice.Checked)
            //    ShowReport(new InvoiceCommSumm(), Detail);
            //else
            //{
            //    if (TypeTypes.GetValue() == MastersType.T_Condominium)
            //        ShowReport(new InvoiceSummaryAdmin(), DSInvSum);
            //    else
            //        ShowReport(new InvoiceSummaryAll(), DSInvSum);
            //}
        }

        private void PrintEnvelopeFromInvoice()
        {
            tabControl1.SelectTab("tpgList");
            return;
            QueryBuilder qb = new QueryBuilder();
            SqlABCommand scc = new SqlABCommand(base.providerType);
            scc.Connection = GlobalInfo.SqlConnection;

            qb.AddSelectAll(PL_SalesInvoices.Name);
            qb.AddFrom(PL_SalesInvoices.Name);

            SqlABParameter sToPrint = new SqlABParameter(base.providerType, "@p1", SqlDbType.Bit);
            sToPrint.Value = 1;
            scc.Parameters.Add(sToPrint);
            qb.AddCompare(PL_SalesInvoices.PrintEnvelope, sToPrint);

            scc.CommandText = qb.Query;
            SqlABDataReader dr = scc.ExecuteReader();

            while (dr.Read())
            {

            }
        }

        protected override void OnDisableControlsForRun()
        {
            rdbAllMaster.Enabled = rdbFromMaster.Checked;
            rdbSelectMaster.Enabled = rdbFromMaster.Checked;
            rdrMasterFrom.Enabled = rdbFromMaster.Checked && rdbSelectMaster.Checked;
            rdrMasterTo.Enabled = rdbFromMaster.Checked && rdbSelectMaster.Checked;

            rdbAllAdmin.Enabled = rdbFromMaster.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;
            rdbSelectAdmin.Enabled = rdbFromMaster.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;

            rdrAdminFrom.Enabled = rdbFromMaster.Checked && rdbSelectAdmin.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;
            rdrAdminTo.Enabled = rdbFromMaster.Checked && rdbSelectAdmin.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;

            cbbType.Enabled = rdbFromMaster.Checked;
        }

        protected override void OnAttachData()
        {
            dbManager = new dbManagerEnvelopePrint("PrintSalesInvoice", base.providerType, ConnectionString);
            TypeTypes = new Sales.Forms.SalesEnumsManager(cbbType, "Plumber.Properties.Resources");
            rdrMasterFrom.AttachCodeType(GlobalInfo.GetCodeType(Properties.Resources.CN_Condominium),
                            dbManager.DBConnection);

            rdrMasterFrom.RadarForm = new RadarMasterForm(MastersType.T_Condominium, dbManager.DBConnection);
            rdrMasterFrom.EnableAddOnFly = false;
            rdrMasterFrom.MustExistData = false;

            rdrMasterTo.AttachCodeType(GlobalInfo.GetCodeType(Properties.Resources.CN_Condominium),
                            dbManager.DBConnection);
            rdrMasterTo.RadarForm = new RadarMasterForm(MastersType.T_Condominium, dbManager.DBConnection);
            rdrMasterTo.EnableAddOnFly = false;
            rdrMasterTo.MustExistData = false;

            rdrAdminFrom.AttachCodeType(GlobalInfo.GetCodeType(Properties.Resources.CN_Administrator),
                dbManager.DBConnection);
            rdrAdminFrom.RadarForm = new RadarMasterForm(MastersType.T_Administrator, dbManager.DBConnection);
            rdrAdminFrom.EnableAddOnFly = false;
            rdrAdminFrom.MustExistData = false;

            rdrAdminTo.AttachCodeType(GlobalInfo.GetCodeType(Properties.Resources.CN_Administrator),
                 dbManager.DBConnection);
            rdrAdminTo.RadarForm = new RadarMasterForm(MastersType.T_Administrator, dbManager.DBConnection);
            rdrAdminTo.EnableAddOnFly = false;
            rdrAdminTo.MustExistData = false;

            drMasters = new DRMasters(dbManager.DBConnection, false);
            drTitle = new DataReaderUpdater(dbManager.DBConnection, PL_Titles.Title, false);

            //ctbFromDate.Today = new DateTime(GlobalInfo.CurrentDate.Year, GlobalInfo.CurrentDate.Month, 1);

            //int year = ctbFromDate.Today.Year;
            //int month = ctbFromDate.Today.Month;
            //int mday = DateTime.DaysInMonth(year, month);
            //ctbToDate.Today = new DateTime(year, month, mday);

            base.OnAttachData();
        }

        protected override void OnBindData()
        {
            BindControl(rdrAdminFrom);
            BindControl(rdrAdminTo);
        }

        private void rdbRangeMaster_CheckedChanged(object sender, EventArgs e)
        {
            rdrMasterFrom.Enabled = rdbSelectMaster.Checked;
            rdrMasterTo.Enabled = rdbSelectMaster.Checked;
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            rdrAdminFrom.Enabled = rdbSelectAdmin.Checked;
            rdrAdminTo.Enabled = rdbSelectAdmin.Checked;
        }

        private void cbbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TypeTypes == null)
                return;

            rdbAllAdmin.Enabled = TypeTypes.GetValue() == MastersType.T_Condominium;
            rdbSelectAdmin.Enabled = TypeTypes.GetValue() == MastersType.T_Condominium;

            rdrAdminFrom.Enabled = rdbSelectAdmin.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;
            rdrAdminTo.Enabled = rdbSelectAdmin.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;

            if (TypeTypes.GetValue() == MastersType.T_Other)
            {
                rdrMasterFrom.Enabled = false;
                rdrMasterFrom.DeattachAll();
                rdrMasterTo.Enabled = false;
                rdrMasterTo.DeattachAll();
            }
            else
            {
                rdrMasterFrom.ReattachCounterType((int)TypeTypes.GetValue());
                rdrMasterFrom.Enabled = rdbSelectMaster.Checked;
                rdrMasterTo.ReattachCounterType((int)TypeTypes.GetValue());
                rdrMasterTo.Enabled = rdbSelectMaster.Checked;

                string nSpace = "";
                switch (TypeTypes.GetValue())
                {
                    case MastersType.T_Condominium:
                        nSpace = "Plumber.Plumber.Masters.Forms.Condominium";
                        break;
                    case MastersType.T_Company:
                        nSpace = "Plumber.Plumber.Masters.Forms.Companies";
                        break;
                    case MastersType.T_Artisan:
                        nSpace = "Plumber.Plumber.Masters.Forms.Artisans";
                        break;
                    default:
                        break;
                }
                ((RadarMasterForm)rdrMasterFrom.RadarForm).ReattachRadar(nSpace, TypeTypes.GetValue());
            }
        }

        private void rdrFrom_Validated(object sender, EventArgs e)
        {
            if (rdrMasterTo.Text.CompareTo(rdrMasterFrom.Text) < 0)
                rdrMasterTo.Text = rdrMasterFrom.Text;
        }

        private void rdrTo_Validated(object sender, EventArgs e)
        {
            if (rdrMasterTo.Text.CompareTo(rdrMasterFrom.Text) < 0)
                rdrMasterFrom.Text = rdrMasterTo.Text;
        }

        private void rdrAdminFrom_Validated(object sender, EventArgs e)
        {
            if (rdrAdminTo.Text.CompareTo(rdrAdminFrom.Text) < 0)
                rdrAdminTo.Text = rdrAdminFrom.Text;
        }

        private void rdrAdminTo_Validated(object sender, EventArgs e)
        {
            if (rdrAdminTo.Text.CompareTo(rdrAdminFrom.Text) < 0)
                rdrAdminFrom.Text = rdrAdminTo.Text;
        }


        private void rdbSumOnly_CheckedChanged(object sender, EventArgs e)
        {
            rdbAllMaster.Enabled = rdbFromMaster.Checked;
            rdbSelectMaster.Enabled = rdbFromMaster.Checked;
            rdrMasterFrom.Enabled = rdbFromMaster.Checked && rdbSelectMaster.Checked;
            rdrMasterTo.Enabled = rdbFromMaster.Checked && rdbSelectMaster.Checked;
            rdbAllAdmin.Enabled = rdbFromMaster.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;
            rdbSelectAdmin.Enabled = rdbFromMaster.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;

            rdrAdminFrom.Enabled = rdbFromMaster.Checked && rdbSelectAdmin.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;
            rdrAdminTo.Enabled = rdbFromMaster.Checked && rdbSelectAdmin.Checked && TypeTypes.GetValue() == MastersType.T_Condominium;

            cbbType.Enabled = rdbFromMaster.Checked;
        }

    }
    internal class dbManagerEnvelopePrint : DBManager
    {
        public dbManagerEnvelopePrint(string name, ApplicationLibrary.SqlConnector.ProviderType providerType, string conString)
            : base(name, providerType, conString)
        {
        }

        protected override string CreateMasterQuery()
        {
            return "";
        }
    }
}
